﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using CombatAutomationTheater;
using GameBoard;

namespace XMLCharSheets
{
    public class PathfinderSystemService : SystemService
    {


        public override void PieceMoved(Guid moverId)
        {
            LightData.CheckLightSources();
            base.PieceMoved(moverId);
        }

        private PathfinderSaveData _pathfinderSaveData = new PathfinderSaveData();
        internal override SystemSaveData GetSaveData()
        {
            return _pathfinderSaveData;
        }

        internal override void OpenSavedData(SystemSaveData systemSavedData)
        {
            _pathfinderSaveData = (PathfinderSaveData) systemSavedData;
        }

        internal PlacedLights LightData 
        {
           get { return _pathfinderSaveData.CurrentPlacedLights; }
        }


        public override void PieceRemoved(CharacterSheet curChar)
        {
            LightData.RemoveLightByOwner(curChar.UniqueCharacterID);
        }

        private PathfinderCustomControlViewModel _customControlViewModel;
        PathfinderVisualUserControl _customControl;
        private LightControlViewModel _primaryLightViewModel;
        private LightControlViewModel _secondaryLightViewModel;

        public override UserControl CustomVisualControlItem()
        {
            _customControlViewModel = new PathfinderCustomControlViewModel();
            _primaryLightViewModel = new LightControlViewModel("Primary Radius");
            _secondaryLightViewModel = new LightControlViewModel("Secondary Radius");
            _customControl = new PathfinderVisualUserControl(_customControlViewModel, _primaryLightViewModel, _secondaryLightViewModel);
            return _customControl;
        }

        public override UserControl CustomControlItem()
        {
            var customControl = new PathfinderControl();
            return customControl;
        }

        public override void OnTerrainFactoryRegisteredChanged(
            TerrainFactoryRegistrationChangedEventArgs terrainFactoryRegistrationChangedEventArgs)
        {
            if (terrainFactoryRegistrationChangedEventArgs.RegType ==
                TerrainFactoryRegistrationChangedEventArgs.RegistrationType.Registered
                &&
                _customControlViewModel.TerrainObjectIsLightSource)
            {
                PathfinderLightSource lightSource = new PathfinderLightSource(_primaryLightViewModel, _secondaryLightViewModel,
                                                                              terrainFactoryRegistrationChangedEventArgs
                                                                                  .ObjectLocation);
                LightData.RegisterLightToOwner(terrainFactoryRegistrationChangedEventArgs.FactoryID, lightSource);
            }
            else if (terrainFactoryRegistrationChangedEventArgs.RegType ==
                     TerrainFactoryRegistrationChangedEventArgs.RegistrationType.Deregistered)
            {
                LightData.RemoveLightByOwner(terrainFactoryRegistrationChangedEventArgs.FactoryID);
            }
        }

        public override void ActiveCharacterChanged()
        {
            LightData.CheckLightSources();
            base.ActiveCharacterChanged();
        }


        public override void OnOpenCompleted()
        {
            //Load light data
            //foreach (var cur in ActiveRosterAsPathfinderCharacters())
            //{
            //    foreach (var curLight in cur.MobileLightSources)
            //    {
            //        RegisterLightToOwner(cur.UniqueCharacterID, curLight);
            //    }
            //}
        }


    }
}
